package com.henrich.ccb.java8stream;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>功能描述：筛选 </p>
 * 筛选，是按照一定的规则校验流中的元素，将符合条件的元素提取到新的流中的操作。
 * <p>修订记录</p>
 *
 * @Author xianghui.zh
 * @Version 1.0 2022/2/21
 **/
public class FilterDemo {
    public static void main(String[] args) {
        // 案例一：筛选出集合中大于7的元素，并打印
        List<Integer> list = Arrays.asList(7, 6, 9, 3, 8, 2, 1);
        list.stream().filter(x -> x > 7).forEach(System.out::println);
        // 案例二：筛选员工中工资高于8000的人，并形成新的集合
        List<Person> personList = new ArrayList<Person>();
        personList.add(new Person("Tom", 8900, 23, "male", "New York"));
        personList.add(new Person("Jack", 7000, 25, "male", "Washington"));
        personList.add(new Person("Lily", 7800, 21, "female", "Washington"));
        personList.add(new Person("Anni", 8200, 24, "female", "New York"));
        personList.add(new Person("Owen", 9500, 25, "male", "New York"));
        personList.add(new Person("Alisa", 7900, 26, "female", "New York"));
        List<String> collect = personList.stream().filter(person -> person.getSalary() > 8000).map(Person::getName).collect(Collectors.toList());
        List<Person> collect1 = personList.stream().filter(person -> person.getSalary() > 8000).collect(Collectors.toList());
        collect.stream().forEach(System.out::println);
        collect1.stream().forEach(System.out::println);
    }
}
